/***********************************************************************
 * Module:  MoveState.java
 * Author:  Nikola
 * Purpose: Defines the Class MoveState
 ***********************************************************************/

package genedit.controller;

import genedit.model.graph.BreakPoint;
import genedit.model.graph.GraphElement;
import genedit.model.graph.Link;
import genedit.view.graph.GraphWorkArea;

import java.awt.Cursor;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.util.ArrayList;

public class MoveState extends State {
	private Point2D start;
	private double globalDiffX = 0;
	private double globalDiffY = 0;

	/** @param e */
	public void mouseReleased(MouseEvent e) {
		// Ovde treba kreirati komandu pomeranja
		Point2D lastPosition = e.getPoint();
		context.pointToUserSpace(lastPosition);

		double xDiff = lastPosition.getX() - start.getX();
		globalDiffX += xDiff;
		double yDiff = lastPosition.getY() - start.getY();
		globalDiffY += yDiff;
		if (context.getView().getModel().getSelectedElements().size() == 1
				&& (context.getView().getModel().getSelectedElements().get(0) instanceof Link)) {
			MovePointCommand newMove = new MovePointCommand(true,globalDiffX,globalDiffY);
			newMove.setDocument(context.getView());
			newMove.setLink((Link) context.getView().getModel().getSelectedElements().get(0));
			newMove.setMoveByX(xDiff);
			newMove.setMoveByY(lastPosition.getY() - start.getY());
			newMove.setIndex(((Link) context.getView().getModel().getSelectedElements().get(0))
					.breakPointAt(start));
			context.getView().getModel().getInvoker().executeCommand(newMove);
		} else {
			MoveCommand newMove = new MoveCommand(true, globalDiffX, globalDiffY);
			newMove.setDocument(context.getView());
			newMove.setMoveByX(xDiff);
			newMove.setMoveByY(lastPosition.getY() - start.getY());
			newMove.setMovedElements(context.getView().getModel().getSelectedElements());
			context.getView().getModel().getInvoker().executeCommand(newMove);
		}
		context.getController().setCurrentState(new SelectionState(context));
		context.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));

		// restart
		globalDiffX = 0;
		globalDiffY = 0;
	}

	/** @param e */
	public void mouseDragged(MouseEvent e) {
		context.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
		context.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
		Point2D lastPosition = e.getPoint();
		context.pointToUserSpace(lastPosition);

		double xDiff = lastPosition.getX() - start.getX();
		globalDiffX += xDiff;
		double yDiff = lastPosition.getY() - start.getY();
		globalDiffY += yDiff;// ukoliko je samo neki link selektovan, onda
								// radimo premestanje connection i breakpointa
								// nad njim
		if (context.getView().getModel().getSelectedElements().size() == 1
				&& (context.getView().getModel().getSelectedElements().get(0) instanceof Link)) {
			MovePointCommand newMove = new MovePointCommand(false,-1,-1);
			newMove.setDocument(context.getView());
			newMove.setLink((Link) context.getView().getModel().getSelectedElements().get(0));
			newMove.setMoveByX(xDiff);
			newMove.setMoveByY(lastPosition.getY() - start.getY());
			newMove.setIndex(((Link) context.getView().getModel().getSelectedElements().get(0))
					.breakPointAt(start));
			context.getView().getModel().getInvoker().executeCommand(newMove);
		} else { // u suprotnom radimo premestanje grupe elemenata
			MoveCommand newMove = new MoveCommand(false, globalDiffX, globalDiffY);
			newMove.setDocument(context.getView());
			newMove.setMoveByX(xDiff);
			newMove.setMoveByY(yDiff);
			newMove.setMovedElements(context.getView().getModel().getSelectedElements());
			context.getView().getModel().getInvoker().executeCommand(newMove);
		}
		start = lastPosition;

	}

	/** @param e */
	public void keyPressed(KeyEvent e) {
		int keycode = e.getKeyCode(); 
		if (keycode == KeyEvent.VK_ESCAPE)
		{
			MoveCommand newMove = new MoveCommand(false, globalDiffX, globalDiffY);
			newMove.setDocument(context.getView());
			newMove.setMoveByX(-globalDiffX);
			newMove.setMoveByY(-globalDiffY);
			newMove.setMovedElements(context.getView().getModel().getSelectedElements());
			context.getView().getModel().getInvoker().executeCommand(newMove);
		
			context.getController().setCurrentState(new SelectionState(context));
			context.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
	
			// restart
			globalDiffX = 0;
			globalDiffY = 0;
			MoveCommand.breakpointsMementoTranslator=null;
		}
	}

	/**
	 * @param context
	 * @param start
	 */
	public MoveState(GraphWorkArea context, Point2D start) {
		super(context);
		this.setStart(start); // odmah u konstruktoru postavimo breakpointe
								// prethodnog stanja
	}

	public Point2D getStart() {
		return start;
	}

	public void setStart(Point2D start) {
		this.start = start;
	}

}